<!-- vue 模板 -->
<template>
  <div id="home">
    <nav-bar fixed>
      <template #title>
        <img class="titleImg" src="@/assets/images/titleicon.png" alt="首页" />
      </template>
    </nav-bar>
    <swipe class="mySwipe" :autoplay="3000" indicator-color="white">
      <swipe-item v-for="(item, index) in banner" :key="index">
        <img :src="item" alt="" />
      </swipe-item>
    </swipe>

    <div class="notice">
      <notice-bar
        left-icon="volume-o"
        text="在代码阅读过程中人们说脏话的频率是衡量代码质量的唯一标准。"
        color="#000000"
        background="#f8f8fa"
      />
    </div>

    <div class="hidden">
      <div class="follow">
        <div
          class="follow-item green"
          v-for="(item, index) in symbolsArray"
          :key="index"
          @click="to('/market', { symbol: item })"
        >
          <div class="follow-item-txt1">
            <!-- BTC<span class="follow-item-small">/USDT</span> -->
            {{ item.title }}
          </div>
          <template v-if="ch[`market.${item.symbol}.detail`]" class="red">
            <div
              :class="
                ch[`market.${item.symbol}.detail`].change > 0 ? 'green' : 'red'
              "
            >
              <div class="follow-item-txt2">
                {{ ch[`market.${item.symbol}.detail`].tick.close }}
              </div>
              <small class="follow-item-txt3"
                >{{ ch[`market.${item.symbol}.detail`].change }}%</small
              >
            </div>
          </template>
        </div>
      </div>
    </div>

    <div class="service">
      <div class="box">
        <img
          src="@/assets/images/home-kefu.png"
          @click="to('/onlineservice')"
          alt=""
        />
        <div>在线客服</div>
      </div>
      <div class="box" @click="to('/lang')">
        <img src="@/assets/images/home-fy.png" alt="" />
        <div>语言切换</div>
      </div>
    </div>

    <div class="quick">
      <div class="home-CzBox-fl" @click="to('/recharge')">
        <div>
          <span>快捷充币</span>
          <div>支持</div>
          <span>BTC,USDT,ETH等</span>
        </div>
        <img src="@/assets/images/home-cz.7bf7730b.png" />
      </div>
      <div class="home-CzBox-fr">
        <div class="home-CzBox-fr-item" @click="to('/market')">
          <img src="@/assets/images/home-qihuo.9e4d1c02.png" />
          <div>期货交易</div>
        </div>
        <div class="home-CzBox-fr-item" @click="to('/helpcenter')">
          <img src="@/assets/images/bangzhu.png" />
          <div>帮助中心</div>
        </div>
      </div>
    </div>

    <div class="activity">
      <img
        v-for="item in activity"
        @click="to(item.to)"
        :key="item.img"
        :src="item.img"
      />
    </div>

    <makeList
      :makeTab="makeTab"
      :nav1="nav1"
      :nav2="nav2"
      :symbolsArray="symbolsArray"
    />
    <Tabbar></Tabbar>
  </div>
</template>

<script>
import { NavBar, Swipe, SwipeItem, NoticeBar } from 'vant'
import Tabbar from '@/components/Tabbar.vue'
import makeList from '@/components/makeList.vue'

import { EventBus } from '@/utils/event-bus'

export default {
  components: {
    Tabbar,
    NavBar,
    Swipe,
    SwipeItem,
    NoticeBar,
    makeList
  },
  data() {
    return {
      ch: {}, //实时数据
      banner: [
        'https://kmadmin.galaxyventures829.xyz/banner/20200929233253088316.jpg',
        'https://kmadmin.galaxyventures829.xyz/banner/20200929233253088316.jpg'
      ],
      // 币种列表
      symbolsArray: [
        { symbol: 'btcusdt', title: 'BTC/USDT' },
        { symbol: 'ethusdt', title: 'ETH/USDT' },
        { symbol: 'eosusdt', title: 'EOS/USDT' },
        { symbol: 'crousdt', title: 'CRO/USDT' },
        { symbol: 'trxusdt', title: 'TRX/USDT' }
      ],
      //  大神跟单、新币申购....
      activity: [
        {
          img: require('@/assets/images/20210406121647456230.jpg'), //图片
          to: '/tradeOrder' //跳转地址
        },
        {
          img: require('@/assets/images/20210324223258901373.png'),
          to: '/wealth'
        }
      ],
      makeTab: ['涨幅榜', '成交量'], //Tab标题
      nav1: ['名称', '最新价', '涨跌幅'], //第一页nav
      nav2: ['名称', '最新价', '24H成交量'] //第二页nav
    }
  },
  created() {
    // vuex里先取出数据
    this.symbolsArray.forEach(item => {
      let d = this.$store.state.websocket.ch[`market.${item.symbol}.detail`]
      if (!d) return
      d.change = parseFloat(
        ((d.tick.close - d.tick.open) / d.tick.open) * 100
      ).toFixed(2)
      this.$set(this.ch, `market.${item.symbol}.detail`, d)
    })
    // 定时查询
    let Interval = setInterval(() => {
      if (this.$store.state.websocket.is_open_socket) {
        this.loadTopMarket()
        // 已连接移除定时器
        clearInterval(Interval)
      }
    }, 500)
  },
  methods: {
    //跳转页面
    to(to, query) {
      this.$router.push({ path: to, query: query })
    },
    //  最新市场概要快照
    loadTopMarket() {
      this.symbolsArray.forEach(item => {
        let ch = `market.${item.symbol}.detail`
        // 消息回调
        EventBus.$on(ch, res => {
          if (!res.data) return
          let d = res.data
          d.change = parseFloat(
            ((d.tick.close - d.tick.open) / d.tick.open) * 100
          ).toFixed(2)
          this.$set(this.ch, res.data.ch, d)
          // console.log(this.ch)
        })
      })
    }
  }
}
</script>

<style scoped lang="less">
#home {
  padding: 46px 0 50px;
  background: #fff;
  .titleImg {
    width: 110px;
  }
  .mySwipe {
    img {
      width: 100vw;
    }
  }
  .notice {
    padding: 0 15px;
    margin: 10px 0;
    .van-notice-bar {
      border-radius: 100px;
    }
  }
  .hidden {
    width: 100%;
    overflow: hidden;
  }
  ::-webkit-scrollbar {
    width: 0 !important ;
    height: 0 !important;
  }
  .follow {
    overflow: scroll;
    white-space: nowrap;
    width: 100%;
    background: #f8f8f8;
    padding: 10px 0;
    .follow-item {
      display: inline-block;
      text-align: center;
      width: 105px;
      padding: 10px 0;
      margin: 0 0 0 15px;
      color: #cd4e65;
      background: #fff;
      -webkit-border-radius: 5px;
      border-radius: 5px;
      font-size: 16px;
      font-weight: 800;
      .follow-item-txt1 {
        color: #333;
        .follow-item-small {
          color: #777;
          font-size: 11px;
          font-weight: 600;
        }
      }
      .follow-item-txt2 {
        margin: 5px auto;
      }
      .follow-item-txt3 {
        font-weight: 600;
      }
    }
    .follow-item:last-child {
      margin: 0 15px 0 15px;
    }
    .green {
      color: #1cad90 !important;
    }
  }
  .service {
    background: #fff;
    display: flex;
    border-bottom: solid 1px #e6eaed;
    .box {
      flex: auto;
      text-align: center;
      font-size: 14px;
      padding: 20px 0;
      color: #213e58;
      font-weight: 700;
      img {
        width: 30px;
        height: 30px;
      }
    }
  }
  .quick {
    background: #f8f8fb;
    padding: 0 10px;
    display: flex;
    align-items: center;
    .home-CzBox-fl {
      display: flex;
      height: 100px;
      overflow: hidden;
      background-color: #fff;
      padding: 10px;
      margin: 10px 0 10px 5px;
      align-items: center;
      justify-content: space-between;
      span {
        font-size: 14px;
        color: #000;
        font-weight: 700;
      }
      img {
        width: 80px;
      }
    }
    .home-CzBox-fr {
      width: 50%;
      .home-CzBox-fr-item {
        display: flex;
        align-items: center;
        margin: 5px;
        background-color: #fff;
        padding: 5px;
        img {
          width: 30px;
          height: 30px;
          margin: 10px;
        }
        div {
          color: #213e58;
          font-weight: 700;
        }
      }
    }
  }
  .activity {
    display: flex;
    flex-direction: column;
    align-items: center;
    img {
      width: 95%;
      height: 90px;
      margin: 10px;
      border-radius: 10px;
    }
  }
}
</style>
